package com.fanxuankai.boot.xxl.job.autoconfigure;

import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Optional;

/**
 * @author fanxuankai
 */
@Configuration
@EnableConfigurationProperties({XxlJobProperties.class})
public class XxlJobAutoConfiguration {
    @Bean
    @ConditionalOnMissingBean(XxlJobSpringExecutor.class)
    public XxlJobSpringExecutor xxlJobSpringExecutor(XxlJobProperties xxlJobProperties) {
        XxlJobSpringExecutor executor = new XxlJobSpringExecutor();
        executor.setAdminAddresses(xxlJobProperties.getAdminAddresses());
        executor.setAccessToken(xxlJobProperties.getAccessToken());
        executor.setAppname(xxlJobProperties.getAppName());
        executor.setAddress(xxlJobProperties.getAddress());
        executor.setIp(xxlJobProperties.getIp());
        Optional.ofNullable(xxlJobProperties.getPort()).ifPresent(executor::setPort);
        executor.setLogPath(xxlJobProperties.getLogPath());
        Optional.ofNullable(xxlJobProperties.getLogRetentionDays()).ifPresent(executor::setLogRetentionDays);
        return executor;
    }
}
